package com.vhall.component.service.account;

import com.google.common.base.Preconditions;
import com.vhall.component.entity.rbac.enums.RoleTypeEnum;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

import static com.vhall.component.plugin.common.exception.BizErrorCode.BIZ_NO_ORGCODE;

/**
 * @author shimingming
 * @since 2023/8/8 9:35
 */
public class BaseUserPermissionService {


    /**
     * 给当前人员赋值
     */
    public Boolean addUserInfo(Object obj) {

        Token token = TokenThreadLocal.getToken();
        Preconditions.checkArgument(token != null, "token信息不能为空！");

        // 获取对象的所有属性
        Field[] fields = obj.getClass().getDeclaredFields();

        // 遍历属性
        for (Field field : fields) {
            try {
                // 设置属性可访问
                field.setAccessible(true);

                if ("accountId".equals(field.getName())) {
                    field.set(obj, token.getAccountId());
                }

                if ("creatorId".equals(field.getName())) {
                    Integer accountId = token.getAccountId();
                    if (field.getType().equals(String.class)) {
                        field.set(obj, accountId.toString());
                    } else if (field.getType().equals(Integer.class)) {
                        field.set(obj, accountId);
                    }
                }

                if ("orgCode".equals(field.getName())) {
                    field.set(obj, token.getOrgCode());
                }

                if ("orgName".equals(field.getName())) {
                    field.set(obj, token.getOrgName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return true;

    }


    /**
     * 判断当前人员是否有权限 查看
     */
    public Boolean checkUserPermission(String orgCodeParam) {

        if (StringUtils.isBlank(orgCodeParam)) {
            throw new BusinessException(BIZ_NO_ORGCODE);
        }

        Token token = TokenThreadLocal.getToken();
        Preconditions.checkArgument(token != null, "token信息不能为空！");
        if (RoleTypeEnum.DEPT_LEVEL.getValue().equals(token.getRoleType())) {
            String orgCode = token.getOrgCode();
            if (!orgCode.equals(orgCodeParam)) {
                throw new BusinessException(BizErrorCode.BIZ_NO_PERMISSION);
            }
        }

        return true;

    }

    /**
     * 获取当前人员查看权限
     * 部门级别人员，返回指定部门；
     * 平台级人员，返回空
     */
    public String getUserPermission() {

        Token token = TokenThreadLocal.getToken();
        Preconditions.checkArgument(token != null, "token信息不能为空！");
        if (RoleTypeEnum.DEPT_LEVEL.getValue().equals(token.getRoleType())) {
            return token.getOrgCode();
        }

        return null;

    }
}
